﻿Imports System.IO
<Serializable()> Public Class IMGWorldFileLayer
    Inherits ImageLayer


    Dim pt1 As New Drawing.PointF
    Dim pt2 As New Drawing.PointF

    '下面存储的是经纬度坐标
    Dim ptlonglat1 As New Drawing.PointF
    Dim ptlonglat2 As New Drawing.PointF

    '为每一个这样类，提供一个辅助器？还是共用一个呢？是个问题
    Dim gHelper As New GEOHelper()

    '为文件提供一个修正后，文件名的存储
    Dim RectifiedFileName As String
    Dim GeoFileName As String

    Dim bRectified As Boolean = False

    '主功能函数
    Public Sub ChangeImage(ByVal fn As String, ByVal p1 As Drawing.PointF, ByVal p2 As Drawing.PointF, ByVal pl1 As Drawing.PointF, ByVal pl2 As Drawing.PointF)

        pt1 = p1
        pt2 = p2
        ptlonglat1 = pl1
        ptlonglat2 = pl2




        '去掉原有的层,但要先保存一些变量
        Dim posOld As Integer

        posOld = mapwin.get_LayerPosition(m_iLayer)


        '先这样用吧
        Me.Dispose()
        mapwin.RemoveLayer(m_iLayer)



        '删除对应的旧的文件
        If RectifiedFileName <> "" Then

            If File.Exists(RectifiedFileName) Then

                File.SetAttributes(RectifiedFileName, FileAttributes.Normal)

                File.Delete(RectifiedFileName)

            End If

        End If

        If GeoFileName <> "" Then

            If File.Exists(GeoFileName) Then

                File.SetAttributes(GeoFileName, FileAttributes.Normal)
                File.Delete(GeoFileName)

            End If

        End If




        If System.IO.File.Exists(fname) Then

            File.SetAttributes(fname, FileAttributes.Normal)
            System.IO.File.Delete(fname)

        End If




        Dim svPath As String = Path.GetDirectoryName(fname) + "\"

        File.Copy(fn, svPath + Path.GetFileName(fn), True)

        fname = svPath + Path.GetFileName(fn)



        '后面的过程是一样的

        '这里默认 x是经度，y是纬度

        Dim wfFileName() As String


        wfFileName = gHelper.GetWorldFile(fname, pt1.X, pt1.Y, pt2.X, pt2.Y, ptlonglat1.Y, ptlonglat1.X, ptlonglat2.Y, ptlonglat2.X)

        Dim img As New MapWinGIS.Image
        RectifiedFileName = wfFileName(0)
        GeoFileName = wfFileName(1)

        If RectifiedFileName <> "" Then
            bRectified = True
        Else
            bRectified = False
        End If



        If bRectified Then

            Dim b As Boolean = img.Open(RectifiedFileName)

            If Not b Then

                System.Windows.Forms.MessageBox.Show("打开文件错误  " + RectifiedFileName)
            End If

        Else

            Dim b As Boolean = img.Open(fname)

            If Not b Then

                System.Windows.Forms.MessageBox.Show("打开文件错误  " + fname)
            End If

        End If




        m_iLayer = mapwin.AddLayer(img, m_ShowLayer)
        mapwin.set_LayerKey(m_iLayer, lname)



        Dim posNew As Integer = mapwin.get_LayerPosition(m_iLayer)

        Dim posAdd As Integer = posNew - posOld

        If posAdd = 0 Then
            Return

        End If

        For i As Integer = 1 To posAdd

            mapwin.MoveLayerDown(posNew)
            posNew = posNew - 1



        Next






    End Sub

    Public Sub ChangeGeo(ByVal p1 As Drawing.PointF, ByVal p2 As Drawing.PointF, ByVal pl1 As Drawing.PointF, ByVal pl2 As Drawing.PointF)


        '保存更改的变量
        pt1 = p1
        pt2 = p2
        ptlonglat1 = pl1
        ptlonglat2 = pl2


        '去掉原有的层,但要先保存一些变量
        Dim posOld As Integer

        posOld = mapwin.get_LayerPosition(m_iLayer)


        '先这样用吧
        Me.Dispose()
        mapwin.RemoveLayer(m_iLayer)


        '删除对应的旧的文件
        If RectifiedFileName <> "" Then

            If File.Exists(RectifiedFileName) Then

                File.Delete(RectifiedFileName)

            End If

        End If

        If GeoFileName <> "" Then

            If File.Exists(GeoFileName) Then

                File.Delete(GeoFileName)

            End If

        End If



        '这里默认 x是经度，y是纬度

        Dim wfFileName() As String


        wfFileName = gHelper.GetWorldFile(fname, pt1.X, pt1.Y, pt2.X, pt2.Y, ptlonglat1.Y, ptlonglat1.X, ptlonglat2.Y, ptlonglat2.X)

        Dim img As New MapWinGIS.Image
        RectifiedFileName = wfFileName(0)
        GeoFileName = wfFileName(1)

        If RectifiedFileName <> "" Then
            bRectified = True
        Else
            bRectified = False
        End If



        If bRectified Then

            Dim b As Boolean = img.Open(RectifiedFileName)

            If Not b Then

                System.Windows.Forms.MessageBox.Show("打开文件错误  " + RectifiedFileName)
            End If

        Else

            Dim b As Boolean = img.Open(fname)

            If Not b Then

                System.Windows.Forms.MessageBox.Show("打开文件错误  " + fname)
            End If

        End If




        m_iLayer = mapwin.AddLayer(img, m_ShowLayer)
        mapwin.set_LayerKey(m_iLayer, lname)



        Dim posNew As Integer = mapwin.get_LayerPosition(m_iLayer)

        Dim posAdd As Integer = posNew - posOld

        If posAdd = 0 Then
            Return

        End If

        For i As Integer = 1 To posAdd

            mapwin.MoveLayerDown(posNew)
            posNew = posNew - 1



        Next





    End Sub

    Protected Sub InitGeo(ByVal p1 As Drawing.PointF, ByVal p2 As Drawing.PointF, ByVal pl1 As Drawing.PointF, ByVal pl2 As Drawing.PointF)

        '这里默认 x是经度，y是纬度

        Dim wfFileName(1) As String


        wfFileName = gHelper.GetWorldFile(fname, pt1.X, pt1.Y, pt2.X, pt2.Y, ptlonglat1.Y, ptlonglat1.X, ptlonglat2.Y, ptlonglat2.X)

        Dim img As New MapWinGIS.Image
        RectifiedFileName = wfFileName(0)

        If RectifiedFileName <> "" Then
            bRectified = True
        End If

        GeoFileName = wfFileName(1)


    

    End Sub




    Public Sub New(ByVal ln As String, ByVal fn As String, ByVal p1 As Drawing.PointF, ByVal p2 As Drawing.PointF, ByVal pl1 As Drawing.PointF, ByVal pl2 As Drawing.PointF)
        MyBase.New(ln, fn)

      


        pt1 = p1
        pt2 = p2
        ptlonglat1 = pl1
        ptlonglat2 = pl2


    End Sub

    '这些都是为了讨厌的VB事件逻辑啊
    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub
    'Public Sub New(ByVal ln As String, ByVal fn As String)
    '    MyBase.New(ln, fn)

    '    If (fn <> "") Then


    '        Return

    '    End If
    '    'ln,fn 的变量已经在基类函数中实现



    'End Sub



    Public Overrides Sub InitNewLayer(ByRef m As AxMapWinGIS.AxMap, ByVal OutPath As String)

        Dim str As String = System.IO.Path.GetFileName(fname)

        System.IO.File.Copy(fname, OutPath + str, True)

        fname = OutPath + str

        InitLayer(m)

    End Sub

    Public Overrides Sub SaveAs(ByVal strPath As String)

        If File.Exists(RectifiedFileName) Then
            File.Copy(RectifiedFileName, strPath + Path.GetFileName(RectifiedFileName), True)
        End If

        If File.Exists(GeoFileName) Then

            File.Copy(GeoFileName, strPath + Path.GetFileName(GeoFileName), True)

        End If

        If File.Exists(fname) Then

            File.Copy(fname, strPath + Path.GetFileName(fname), True)
        End If





    End Sub

    Public Overrides Sub ReLoad(ByRef m As AxMapWinGIS.AxMap, ByVal fPath As String)

        fname = fPath + Path.GetFileName(fname)
        RectifiedFileName = fPath + Path.GetFileName(RectifiedFileName)

        GeoFileName = fPath + Path.GetFileName(GeoFileName)


        Me.InitLayer(m)


    End Sub

    Public Overrides Sub InitLayer(ByRef m As AxMapWinGIS.AxMap)


        mapwin = m

        Me.InitGeo(pt1, pt2, ptlonglat1, ptlonglat2)

        Dim img As New MapWinGIS.Image
        If bRectified Then

            Dim b As Boolean = img.Open(RectifiedFileName)

            If Not b Then

                System.Windows.Forms.MessageBox.Show("打开文件错误  " + RectifiedFileName)
            End If

        Else

            Dim b As Boolean = img.Open(fname)

            If Not b Then

                System.Windows.Forms.MessageBox.Show("打开文件错误  " + fname)
            End If

        End If

        

        m_iLayer = mapwin.AddLayer(img, True)

        mapwin.set_LayerKey(m_iLayer, lname)


    End Sub

End Class
